#ifndef WAVEFILE_H

#define WAVEFILE_H

#include <afx.h>

class CFile;

class CWaveFile: public CFile
	{
public: // Konstanten
	enum {OK=0, OpenError=-1, FormatError=-2, ReadError=-3, WriteError=-4, ModeError=-5, MemError=-6} ;

private:
	bool IsOpen;
	bool ReadOnly;
	bool HeaderChanged;
	bool High;
	bool isStereo;
	long Freq;
	long nSamples;
	long DataOffs;
	long BufSize; // in byte
	bool BufChanged; // falls ein Schreibvorgang stattfand
	long BufPos; // in Samples
	WORD *WBuf;
	BYTE *BBuf;

public:
	CWaveFile();
	CWaveFile(const char *FileName, bool AReadOnly);
	~CWaveFile();

	// zum Lesen: 
	int Open(const char *FileName, bool AReadOnly);
	long Samples() { return IsOpen ? nSamples : ModeError; } 
	long SampleFreq() { return IsOpen ? Freq : ModeError; }
	bool Stereo() { return IsOpen && isStereo; }
	int Bits() { return IsOpen ? (High ? 16 : 8) : ModeError; }
	int BytePerSample() { return (High ? 2 : 1) * (isStereo ? 2 : 1); }
	long FileSamplePos(long pos) { return DataOffs + pos * BytePerSample(); }

	int GetSample(int &x, long Pos, bool Right = false);
	int SetLength(long NewLen);
	
	// zum Schreiben
	int Create(const char *FileName, long SampleFreq, bool Stereo, int Bits, long Samples);
	int PutSample(int x, long Pos, bool Right = false); // bei Stereo nacheinander Links und Rechts

	void Close() virtual;

private:
	int WriteHeader();
	void DeleteBuf();
	bool CreateBuf();
	int WriteBuf();
	int GetBuf(long Pos);
	bool CheckBufPos(long Pos)
		{ return (BufPos >= 0 && Pos >= BufPos && Pos < BufPos + (BufSize / BytePerSample())); }
	} ;


#endif // WAVEFILE_H







